#!/usr/bin/env python3
import yaml
import sys

def _find_index(yaml_data, key_under_paths, keytype, content, match_type="exact"):
    paths_map = yaml_data.get('paths', {})
    target_map = paths_map.get(key_under_paths, {})
    result_dict = {}
    
    for entry_index, entry_data in target_map.items():
        if match_type == "partial":
            if content in entry_data.get(keytype, ''):
                result_dict[entry_index] = entry_data
        else:
            if entry_data.get(keytype) == content:
                result_dict[entry_index] = entry_data
    
    if match_type == "partial":
        return list(result_dict.keys()), target_map
    else:
        if len(result_dict) == 1:
            return list(result_dict.keys())[0], target_map
        return None, target_map

def debug_eval_by():
    # 加载YAML文件
    yaml_file = "p814.yaml"
    with open(yaml_file, 'r') as f:
        yaml_data = yaml.safe_load(f)
    
    # Train 312的source: -t -tz 74 -g f100 -tp 410
    train_source = "-t -tz 74 -g f100 -tp 410"
    cmd_parts = train_source.split()
    
    # 提取参数
    tz_value = next((cmd_parts[i + 1] for i, part in enumerate(cmd_parts) if part == "-tz" and i + 1 < len(cmd_parts)), None)
    g_value = next((cmd_parts[i + 1] for i, part in enumerate(cmd_parts) if part == "-g" and i + 1 < len(cmd_parts)), None)
    tp_value = next((cmd_parts[i + 1] for i, part in enumerate(cmd_parts) if part == "-tp" and i + 1 < len(cmd_parts)), None)
    
    print(f"Train 312 parameters:")
    print(f"  tz_value: {tz_value}")
    print(f"  g_value: {g_value}")
    print(f"  tp_value: {tp_value}")
    print()
    
    # 搜索匹配的eval条目
    print("Searching for eval entries...")
    
    # 搜索 -tz 74
    tz_indices, _ = _find_index(yaml_data, 'eval', 'source', f"-tz {tz_value}", match_type="partial")
    print(f"  Found {len(tz_indices)} entries with -tz {tz_value}: {tz_indices}")
    
    # 搜索 -g f100
    g_indices, _ = _find_index(yaml_data, 'eval', 'source', f"-g {g_value}", match_type="partial")
    print(f"  Found {len(g_indices)} entries with -g {g_value}: {g_indices}")
    
    # 搜索 -tp 410
    tp_indices, _ = _find_index(yaml_data, 'eval', 'source', f"-tp {tp_value}", match_type="partial")
    print(f"  Found {len(tp_indices)} entries with -tp {tp_value}: {tp_indices}")
    
    print()
    
    # 计算交集
    tz_set = set(tz_indices)
    g_set = set(g_indices)
    tp_set = set(tp_indices)
    common_indices = tz_set & g_set & tp_set
    
    print(f"Intersection calculation:")
    print(f"  tz_set: {tz_set}")
    print(f"  g_set: {g_set}")
    print(f"  tp_set: {tp_set}")
    print(f"  common_indices: {common_indices}")
    
    # 检查eval 509是否应该被包含
    print(f"\nChecking eval 509:")
    eval_509_source = yaml_data['paths']['eval']['509']['source']
    print(f"  eval 509 source: {eval_509_source}")
    print(f"  Contains -tz {tz_value}: {'-tz ' + tz_value in eval_509_source}")
    print(f"  Contains -g {g_value}: {'-g ' + g_value in eval_509_source}")
    print(f"  Contains -tp {tp_value}: {'-tp ' + tp_value in eval_509_source}")
    
    # 显示所有eval条目的source
    print(f"\nAll eval entries:")
    for idx, entry in yaml_data['paths']['eval'].items():
        source = entry.get('source', '')
        if f"-tz {tz_value}" in source and f"-g {g_value}" in source and f"-tp {tp_value}" in source:
            print(f"  {idx}: {source} [MATCHES ALL]")
        elif f"-tz {tz_value}" in source:
            print(f"  {idx}: {source} [MATCHES TZ]")
        elif f"-g {g_value}" in source:
            print(f"  {idx}: {source} [MATCHES G]")
        elif f"-tp {tp_value}" in source:
            print(f"  {idx}: {source} [MATCHES TP]")
        else:
            print(f"  {idx}: {source} [NO MATCHES]")

if __name__ == "__main__":
    debug_eval_by() 